package com.twentytwok.hospitalsystem22K.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.twentytwok.hospitalsystem22K.common.Code;
import com.twentytwok.hospitalsystem22K.common.PageVo;
import com.twentytwok.hospitalsystem22K.common.R;
import com.twentytwok.hospitalsystem22K.mapper.NewsMapper;
import com.twentytwok.hospitalsystem22K.pojo.AdminUser;
import com.twentytwok.hospitalsystem22K.pojo.News;
import com.twentytwok.hospitalsystem22K.service.AdminUserService;
import com.twentytwok.hospitalsystem22K.service.NewsService;
import com.twentytwok.hospitalsystem22K.vo.NewsQueryVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;

/**
 * 新闻管理模块的服务层接口实现类
 *
 * @author 鑫小小
 * @data 2021年11月11日10:10:02
 */

@Service("newsService")
public class NewsServiceImpl implements NewsService {

    @Resource
    private NewsMapper newsMapper;

    @Resource
    private AdminUserService adminUserService;

    @Override
    public List<News> findNews() {
        return newsMapper.selectAllNews();
    }

    @Override
    public R addNews(News news) {
        news.setTime(new Date());
        System.out.println(news);
      int result = newsMapper.addNews(news);

        if (result ==1) {
            return new R(Code.OK,"添加成功");
        } else {
            return new R(Code.ERROR, "添加失败");
        }

    }

    @Override
    public R deleteNews(Integer id) {
        News news = new News();

        news.setId(id);
        news.setDelete(1);
        int result =  newsMapper.modifyNews(news);

        if (result ==1) {
            return new R(Code.OK,"删除成功");
        } else {
            return new R(Code.ERROR, "删除失败");
        }
    }

    @Override
    public R modifyNews(News news) {
        int result =  newsMapper.modifyNews(news);

        if (result ==1) {
            return new R(Code.OK,"修改成功");
        } else {
            return new R(Code.ERROR, "修改失败");
        }
    }

    @Override
    public R getNewsById(Integer id) {
        List<News> news = newsMapper.selectNewsById(id);

        if (news.size() > 0 ) {
            return new R(Code.OK,"查询成功",news.get(0));
        }else {
            return new R(Code.ERROR, "查询失败");
        }


    }

    @Override
    public R listNewsByTime(NewsQueryVo newsQueryVo) {
        PageHelper.startPage(newsQueryVo.getPage(), newsQueryVo.getLimit());

        List<News> news = newsMapper.selectNewsByTime();

        PageInfo<News> newsPageInfo = new PageInfo<>(news);

        return new R(Code.OK, "根据时间排序查询成功", (int) newsPageInfo.getTotal(), newsPageInfo.getList());
    }

    @Override
    public R getNewsAsPie() {
       List<AdminUser> adminUsers = adminUserService.getAllAdminUser();
       List<Map<String,Object>> lists = new ArrayList<>();

        for (AdminUser adminUser : adminUsers) {
            Integer admin_user_id = adminUser.getId();
            Map<String,Object> newsMap = new HashMap<>();
            int result =  newsMapper.selectNewsAsPie(admin_user_id);

            newsMap.put("name", adminUser.getName());
            newsMap.put("value", result);

            lists.add(newsMap);
        }

        if (lists.size() >0) {
            return new R(Code.OK, "查询成功",lists);
        } else {
            return new R(Code.ERROR, "查询失败");
        }
    }

    @Override
    public R getDataNews(NewsQueryVo newsQueryVo) {
        //mybatis 分页插件
        PageHelper.startPage(newsQueryVo.getPage(), newsQueryVo.getLimit());


        List<News> news = newsMapper.selectByParams(newsQueryVo);

        PageInfo<News> newsPageInfo = new PageInfo<>(news);

        return new R(Code.OK, "查询新闻分页数据", (int) newsPageInfo.getTotal(), newsPageInfo.getList());
    }
}
